using System;
using System.Collections;

using Weblog.Domain;

using Evaluant.Uss;
using Evaluant.Uss.ObjectContext;

namespace Weblog.Services
{
	public class EussService : IService
	{
		private ObjectContext oc;

		public EussService(ObjectContext oc)
		{
			this.oc = oc;
		}

		public IList GetAllBlogs()
		{
			return oc.Load(typeof(Blog));
		}

		public Blog GetBlogWithTitle(string title)
		{
			return oc.LoadSingle(typeof(Blog), String.Format("[ Title = '{0}' ]", title)) as Blog; 
		}

		public IList GetPostsFromBlogWithPublished(Blog blog, DateTime published)
		{
			Query query = new Query(typeof(Blog));
			query.Constraint = String.Format("[ Title = '{0}'].Posts[ Published = #{1}# ]", blog.Title, published.ToString("s"));
			query.OrderBy.Add("Published desc");

			return oc.Load(query);
		}

		public IList GetPostsFromBlog(Blog blog)
		{
			Query query = new Query(typeof(Blog));
			query.Constraint = String.Format("[ Title = '{0}'].Posts", blog.Title);
			query.OrderBy.Add("Published desc");

			return oc.Load(query);
		}
	}
}
